<?php
session_start();
// Clear all previous messages.
if($_SESSION['log-err']) session_unregister('log-err');
if($_SESSION['log-info']) session_unregister('log-info');

$user_id = $_POST['studentId'];
$user_name = $_POST['studentName'];



include('./dbconnection.php');
// Check whether student is already registerd.
$query_check = "SELECT * FROM student WHERE student_id = '$user_id'";


if($students_exsist = mysql_query($query_check)){
	if(mysql_num_rows($students_exsist)>0){
		// Student is already registered.
		$_SESSION['log-err'] = "Student Already Exists.";
	}else{
		//add student to the database
		$query_add = "INSERT INTO student ( student_id, name )
                       VALUES  ('$user_id','$user_name' )";
		echo "$query_add";
		if(mysql_query($query_add)){
			// student add successful.
			$_SESSION['log-info'] = "Add Student Successfully.";
		}else{
			// SQL error occured.
			$_SESSION['log-err'] = "Sorry. Error occured. Please try again.";
		}
	}
}


else{
	// Incomplet form submission.
	$_SESSION['log-err'] = "All the fields should be filled.";

}
$_SESSION['page'] = "addstudent.php";
header('Location: ../');


?>